package cn.edu.nuc.zyb.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import cn.edu.nuc.zyb.client.AdminClient;
import cn.edu.nuc.zyb.model.Goods;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
import java.util.List;
import java.awt.event.ActionEvent;

public class AdminStore extends JFrame {

	private JPanel contentPane;
	private JTextField goodsId;
	public JFrame frame;
	public List<Goods> list;
	public static int maxrow=0;

	/**
	 * Launch the application.
	 */

	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { AdminStore frame = new
	 * AdminStore(new AdminLogin()); frame.setVisible(true); } catch (Exception
	 * e) { e.printStackTrace(); } } }); }
	 */
	/**
	 * Create the frame.
	 */
	public AdminStore(JFrame frame, String name) {
		setTitle("中北商场后台管理系统--当前用户:" + name);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
		panel.setLayout(new GridLayout(1, 1, 0, 0));

		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(人民币)");
		model.addColumn("库存");
		// model.addColumn("操作");

		File file = new File("D:\\store\\goods");

		File[] files = file.listFiles();

		for (int i = 0; i < files.length; i++) {
			try {
				FileInputStream fis = new FileInputStream("D:\\store\\goods\\" + files[i].getName());
				ObjectInputStream ois = new ObjectInputStream(fis);
				Goods goods = (Goods) ois.readObject();
				model.addRow(new String[] { String.valueOf(goods.getGid()), goods.getGoodsName(),
						String.valueOf(goods.getPrice()), String.valueOf(goods.getNum()) });

				ois.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
//		maxrow = model.getRowCount();

		JTable table = new JTable(model);

		JScrollPane pane = new JScrollPane(table);

		panel.add(pane);
		contentPane.add(panel);

		JButton button = new JButton("添加商品");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AdminAdd add = new AdminAdd(AdminStore.this, name);
				add.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				add.setVisible(true);
			}
		});
		button.setBounds(386, 45, 93, 23);
		contentPane.add(button);

		JButton button_1 = new JButton("修改商品");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int row = table.getSelectedRow();

				if (row == -1) {
					JOptionPane.showMessageDialog(AdminStore.this, "请选择要删除的行");

				}
				String goodname = (String) table.getValueAt(row, 1);// 会把 选中行的
																	// 第二个元素取出来

				AdminUpdate u = new AdminUpdate(goodname, AdminStore.this, name);
				u.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				u.setVisible(true);
			}
		});
		button_1.setBounds(489, 45, 93, 23);
		contentPane.add(button_1);

		JButton button_2 = new JButton("删除选中商品");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 得到当前选中商品项
				int row = table.getSelectedRow();
				String goodname = (String) table.getValueAt(row, 1);
				System.out.println(goodname);
				if (row == -1) {
					JOptionPane.showMessageDialog(AdminStore.this, "请选择要删除的行");

				} else {
					JOptionPane.showConfirmDialog(null, "确定要删除" + table.getValueAt(row, 1) + "么?");
					AdminClient ac = new AdminClient();
					ac.deleteGoods(goodname);
					if (ac.acceptMsg()) {
						JOptionPane.showMessageDialog(null, "删除成功");
						model.removeRow(row);
					} else {
						JOptionPane.showMessageDialog(null, "删除失败");
					}

				}

			}
		});
		button_2.setBounds(587, 45, 119, 23);
		contentPane.add(button_2);

		JButton button_3 = new JButton("退出登录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(true);
				AdminStore.this.setVisible(false);
			}
		});
		button_3.setBounds(613, 6, 93, 23);
		contentPane.add(button_3);

		JLabel label = new JLabel("当前在线用户数: 5");
		label.setBounds(10, 10, 162, 15);
		contentPane.add(label);

		JLabel lblid = new JLabel("商品编号:");
		lblid.setBounds(10, 53, 54, 15);
		contentPane.add(lblid);

		goodsId = new JTextField();
		goodsId.setBounds(68, 50, 104, 21);
		contentPane.add(goodsId);
		goodsId.setColumns(10);

		JButton button_4 = new JButton("搜索");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				model.setNumRows(0);
				int id = Integer.valueOf(goodsId.getText());
				AdminClient ac = new AdminClient();
				ac.searchGoods(id);
				Goods good = ac.acceptSearchGoods();
				String gid = String.valueOf(good.getGid());
				String price = String.valueOf(good.getPrice());
				String num = String.valueOf(good.getNum());
				System.out.println(gid + price + num);

				model.addRow(new String[] { gid, good.getGoodsName(), price, num });

				// String[] msgs = msg.split(",");
				// model.addRow(new String[] { msgs[0], msgs[1], msgs[2],
				// msgs[3] });
			}
		});
		button_4.setBounds(180, 49, 93, 23);
		contentPane.add(button_4);
	}
}
